<template>
  <el-dialog
    :title="dataForm.stateExplain"
    :visible.sync="editDialogData.visible"
    :close-on-click-modal="false"
    width="80%"
    top="8vh"
  >
    <el-row>
      <el-form :model="dataForm" ref="dataForm" label-width="130px">
        <div class="cjrwBg">
          <el-row>
            <el-col :span="24">
              <el-col :span="8">
                <el-form-item label="发起名称" prop="faqiName">
                  <el-input
                    disabled
                    v-model="dataForm.faqiName"
                    placeholder="发起名称"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="16">
                <el-form-item label="任务有效期" required>
                  <el-col :span="11">
                    <el-date-picker
                      v-model="dataForm.rwYxqq"
                      type="date"
                      disabled
                      value-format="yyyy-MM-dd"
                      prop="rwYxqq"
                      placeholder="开始日期"
                      style="width: 100%"
                    ></el-date-picker>
                  </el-col>
                  <el-col :span="2">
                    <div class="text-center">至</div>
                  </el-col>
                  <el-col :span="11">
                    <el-date-picker
                      disabled
                      v-model="dataForm.rwYxqz"
                      type="date"
                      value-format="yyyy-MM-dd"
                      prop="rwYxqz"
                      placeholder="结束日期"
                      style="width: 100%"
                    ></el-date-picker>
                  </el-col>
                </el-form-item>
              </el-col>
            </el-col>
            <el-col :span="24">
              <el-col :span="8">
                <el-form-item label="填报结束日期" prop="signDate">
                  <el-date-picker
                    v-model="dataForm.signDate"
                    disabled
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="填报结束日期"
                    style="width: 100%"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="模板名称" prop="templateId">
                  <el-select
                    v-model="dataForm.templateId"
                    disabled
                    placeholder="模板名称"
                    class="xiaLa"
                    style="width: 107%"
                    :clearable="true"
                  >
                    <el-option
                      v-for="(item, index) in categoryList"
                      :key="index"
                      :label="item.templateName"
                      :value="item.id"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-col>
            <el-col>
              <el-col :span="24">
                <el-button
                  type="text"
                  style="margin-left: 30px"
                  icon="el-icon-user-solid"
                ></el-button>
                被考核人：{{ dataForm.ryName }}
              </el-col>
            </el-col>
          </el-row>
        </div>
      </el-form>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-table
          style="width: 100%; margin-top: 20px"
          :data="dataList"
          border
          v-loading="loading"
        >
          <el-table-column
            type="index"
            width="80"
            header-align="center"
            align="center"
            label="序号"
          ></el-table-column>
          <el-table-column
            prop="batchNumber"
            width="60"
            header-align="center"
            align="center"
            label="批次"
          ></el-table-column>
          <el-table-column
            prop="renwuName"
            header-align="center"
            align="center"
            label="任务名称"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input
                v-if="!disabled"
                v-model="scope.row.renwuName"
                placeholder="任务名称"
                @change="
                  changeValue(scope.row.renwuName, 'renwuName', scope.row.id)
                "
              />
              <span v-else>{{ scope.row.renwuName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="难度系数"
            header-align="center"
            align="center"
          >
            <el-table-column
              prop="zycd"
              header-align="center"
              align="center"
              label="重要程度"
            >
              <template slot="header" slot-scope="scope">
                <span style="margin-right: 15px">重要程度</span>
                <el-tooltip class="item" effect="dark" placement="top-start">
                  <div slot="content">
                    说明：<br />
                    1. 重要程度高，是指重点任务对集团层面有重要影响；<br />
                    2. 重要程度较高，是指重点任务对公司层面有重要影响；<br />
                    3. 重要程度一般，是指重点任务对部门层面有重要影响；<br />
                  </div>
                  <i class="el-icon-info" style="color: #3e51a3"></i>
                </el-tooltip>
              </template>
              <template slot-scope="scope">
                <el-select
                  v-if="!disabled"
                  v-model="scope.row.zycd"
                  placeholder="请选择重要程度"
                  @change="changeValue(scope.row.zycd, 'zycd', scope.row.id)"
                >
                  <el-option
                    v-for="(item, index) in chengduList"
                    :label="item.name"
                    :value="item.value"
                    :key="index"
                  ></el-option>
                </el-select>
                <span v-else>{{ scope.row.zycd }}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="wcnd"
              header-align="center"
              align="center"
              label="完成难度"
            >
              <template slot="header" slot-scope="scope">
                <span style="margin-right: 15px">完成难度</span>
                <el-tooltip class="item" effect="dark" placement="top-start">
                  <div slot="content">
                    说明：<br />
                    1. 完成难度高，是指重点任务创新性强、协调难度大；<br />
                    2.
                    完成难度较高，是指重点任务有一定创新性、需要争取政策支持；<br />
                    3.
                    完成难度一般，是指重点任务有一定难度，但有专业团队支持，本部门能够独立承担工作；<br />
                  </div>
                  <i class="el-icon-info" style="color: #3e51a3"></i>
                </el-tooltip>
              </template>
              <template slot-scope="scope">
                <el-select
                  v-if="!disabled"
                  @change="changeValue(scope.row.wcnd, 'wcnd', scope.row.id)"
                  v-model="scope.row.wcnd"
                  placeholder="请选择完成难度"
                >
                  <el-option
                    v-for="(item, index) in chengduList"
                    :label="item.name"
                    :value="item.value"
                    :key="index"
                  ></el-option>
                </el-select>
                <span v-else>{{ scope.row.wcnd }}</span>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column
            header-align="center"
            align="center"
            label="任务有效期"
            min-width="400"
          >
            <el-table-column
              prop="rwYxqq"
              align="center"
              label="开始时间"
              width="200"
            >
              <template slot-scope="scope">
                <el-date-picker
                  v-if="!disabled"
                  @change="
                    changeValue(scope.row.rwYxqq, 'rwYxqq', scope.row.id)
                  "
                  type="date"
                  placeholder="选择日期"
                  v-model="scope.row.rwYxqq"
                  style="width: 100%"
                  value-format="yyyy-MM-dd"
                ></el-date-picker>
                <span v-else>{{ scope.row.rwYxqq }}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="rwYxqz"
              align="center"
              label="结束时间"
              width="200"
            >
              <template slot-scope="scope">
                <el-date-picker
                  v-if="!disabled"
                  @change="
                    changeValue(scope.row.rwYxqz, 'rwYxqz', scope.row.id)
                  "
                  type="date"
                  placeholder="选择日期"
                  v-model="scope.row.rwYxqz"
                  value-format="yyyy-MM-dd"
                  style="width: 100%"
                ></el-date-picker>
                <span v-else>{{ scope.row.rwYxqz }}</span>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column
            prop="weightset"
            header-align="center"
            align="center"
            label="权重"
          >
            <template slot-scope="scope">
              <el-input
                v-if="!disabled"
                @change="
                  changeValue(scope.row.weightset, 'weightset', scope.row.id)
                "
                type="number"
                min="0"
                max="100"
                oninput="value=value.replace(/[^\d]/g,0);if(isNaN(value))value=0;if(value>100)value=100;
                                      if(value<0)value=0;if(value.charAt(0)==0&&value.charAt(1)!='')value=0"
                v-model="scope.row.weightset"
                placeholder="请输入内容"
              />
              <span v-else>{{ scope.row.weightset }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="workTarget"
            header-align="center"
            align="center"
            label="工作目标/成效"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input
                v-if="!disabled"
                @change="
                  changeValue(scope.row.workTarget, 'workTarget', scope.row.id)
                "
                v-model="scope.row.workTarget"
                placeholder="工作目标/成效"
              />
              <span v-else>{{ scope.row.workTarget }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="auditStatusDsc"
            header-align="center"
            align="center"
            width="120"
            label="状态"
          >
          </el-table-column>
          <el-table-column
            label="操作"
            width="60"
            header-align="center"
            align="center"
            v-if="!disabled"
          >
            <template slot="header">
              <el-button
                type="primary"
                icon="el-icon-plus"
                circle
                :disabled="dataForm.stateExplain != '未提交'"
                @click="addBtn()"
              ></el-button>
            </template>
            <template slot-scope="scope">
              <el-button
                :disabled="dataForm.stateExplain != '未提交'"
                type="danger"
                icon="el-icon-minus"
                circle
                @click="delBtn(scope.row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!--  选择人员信息-->
    <UserCommonSelect ref="usercomment" @submitfy="submitfy"></UserCommonSelect>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" plain @click="backBtn">关闭</el-button>
    </span>
  </el-dialog>
</template>
<script>
import UserCommonSelect from "@/views/hetongrenwu/components/usercomment-select";
import {
  getDetailListnd,
  removeDetailById,
  updateDetailById,
  addDetailnd,
} from "@/api/hetongrenwu/zhongdian/renwuchuangjian";
import { effective } from "@/api/hetongrenwu/hetongyeji/hetongnd";

export default {
  components: {
    UserCommonSelect,
  },
  props: {
    editDialogData: {},
  },
  data() {
    return {
      shouyuerendataList: [],
      dataList: [],
      dataForm: {},
      categoryList: [],
      jsonData: "",
      index: 1,
      id: "",
      zdrwBkhUsers: [],
      chengduList: [
        { name: "高", value: "高" },
        { name: "较高", value: "较高" },
        { name: "一般", value: "一般" },
      ],
      loading: false,
      disabled: Boolean,
    };
  },
  watch: {
    editDialogData: {
      handler(newVal, oldVal) {
        if (newVal.visible) {
          console.log(newVal.rows)
          this.dataForm = newVal.rows;
          this.fngetDetailListdepid(newVal.rows.id);
          // 0 (创建中)  1(填报中)  4(考核人驳回)   6(汇总驳回)
          if (
            newVal.rows.stateExplain == "未提交"
          ) {
            this.disabled = false;
          } else {
            this.disabled = true;
          }
        }
      },
      immediate: true,
      deep: true,
    },
  },
  created() {
    this.fneffective();
  },
  methods: {
    backBtn() {
      this.dataList = [];
      this.shouyuerendataList = [];
      this.dataForm = {};
      this.editDialogData.visible = false;
      this.$emit("editDialogDataChildren",'返回')
    },
    addBtn() {
      var str = this.getNowTime();
      var str2 = this.getNowTime2();
      var item = {
        renwuName: "任务名称" + this.index,
        bkhName: "1",
        zycd: "较高",
        wcnd: "较高",
        weight: "100",
        rwYxqq: str,
        rwYxqz: str2,
        workTarget: "无",
        dataProvider: "1",
        providerName: "1",
        index: this.index,
        zbfqndId: this.dataForm.id,
        renwuId: this.dataForm.renwuId,
        ndId:this.dataForm.cjId,
      };
      this.index++;
      addDetailnd(item).then((res) => {
        this.$message.closeAll();
        this.$message.success("添加成功");
        this.fngetDetailListdepid(this.dataForm.id);
      });
    },
    delBtn(row) {
      removeDetailById(row.id).then((res) => {
        this.$message.closeAll();
        this.$message.success("删除成功");
        this.fngetDetailListdepid(this.dataForm.id);
      });
    },
    fngetDetailListdepid(id) {
      this.loading = true;
      getDetailListnd({ id }).then((response) => {
        this.dataList = response.data.data;
        this.loading = false;
      });
    },
    changeValue(val, type, id) {
      console.log(val, type, id);
      var params = {
        [type]: val,
        id: id,
      };
      console.log(params);
      updateDetailById(params).then((res) => {
        this.$message.closeAll();
        this.$message.success("修改成功");
        this.fngetDetailListdepid(this.dataForm.id);
      });
    },
    addUser(type) {
      this.jsonData = JSON.stringify(this.shouyuerendataList);
      this.$nextTick(() => {
        this.$refs.usercomment.init("12", type, this.jsonData, "htcj");
      });
    },
    submitfy(data) {
      this.shouyuerendataList = data.tableDataUser;
      console.log(JSON.stringify(this.shouyuerendataList));
      var obj = {};
      this.shouyuerendataList.forEach((ele) => {
        var user = {};
        user.userId = ele.userId;
        user.userName = ele.userCode;
        user.ryName = ele.userName;
        user.deptId = ele.deptId;
        user.userDeptName = ele.deptName;
        user.userZw = ele.userZw;
        user.userLx = "自拟";
        user.userZj = ele.userZj;
        this.zdrwBkhUsers.push(user);
      });
    },
    fneffective() {
      effective({
        tagName: "重点任务",
      }).then((res) => {
        this.categoryList = res.data.data;
      });
    },
    getNowTime: function () {
      let dateTime;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      dateTime = yy + "-" + mm + "-" + dd;
      return dateTime;
    },
    getNowTime2: function () {
      let dateTime;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 2;
      let dd = new Date().getDate();
      dateTime = yy + "-" + mm + "-" + dd;
      return dateTime;
    },
  },
};
</script>
<style scoped>
.cjrwBg {
  border-radius: 16px;
  -webkit-box-shadow: 5px 5px 10px #e3e3ea, -5px -5px 10px #ffffff;
  box-shadow: 5px 5px 10px #e3e3ea, -5px -5px 10px #ffffff;
  background: #fbfbfd;
  padding: 10px 30px 30px 30px;
}
</style>
